package br.com.netomarin.bundesliga.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import br.com.netomarin.bundesliga.persistence.Match;
import br.com.netomarin.bundesliga.persistence.PMF;
import br.com.netomarin.bundesliga.persistence.Player;

public class PlayerModel {

	private PlayerModel() {}
	
	public static int savePlayers(ArrayList<Player> players) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			pm.makePersistentAll(players);
		} finally {
			pm.close();
		}
		
		return players.size();
	}
	
	public static void savePlayer(Player player) {
		PersistenceManager pm = null;
		pm = JDOHelper.getPersistenceManager(player);
		if (pm == null)
			pm = PMF.get().getPersistenceManager();

		try {
			pm.makePersistent(player);
		} finally {
			pm.close();
		}
	}
	
	public static Player loadPlayerByName(String name) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Player p = null;
		try {
			p = pm.getObjectById(Player.class, name);
		} catch (JDOObjectNotFoundException e) {
			return new Player(name);
		}
		
		return p ;
	}
	
	public static void processPlayersMatch(Match m) {
		processPlayersMatch(m, false);
	}

	public static void processPlayersMatch(Match m, boolean initialData) {
		Player homeP1 = null;
		if (m.getHomeP1() != null && !m.getHomeP1().equalsIgnoreCase("N/A") && !m.getHomeP1().isEmpty())
				homeP1 = loadPlayerByName(m.getHomeP1());
		Player homeP2 = null;
		if (m.getHomeP2() != null && !m.getHomeP2().equalsIgnoreCase("N/A") && !m.getHomeP2().isEmpty())
				homeP2 = loadPlayerByName(m.getHomeP2());
		Player visitorP1 = null;
		if (m.getVisitorP1() != null && !m.getVisitorP1().equalsIgnoreCase("N/A") && !m.getVisitorP1().isEmpty())
				visitorP1 = loadPlayerByName(m.getVisitorP1());
		Player visitorP2 = null;
		if (m.getVisitorP2() != null && !m.getVisitorP2().equalsIgnoreCase("N/A") && !m.getVisitorP2().isEmpty())
				visitorP2 = loadPlayerByName(m.getVisitorP2());
		
		int pointsHome = 0, pointsVisitor = 0;
		if (m.getScoreHomeTeam() > m.getScoreVisitorTeam()) {
			pointsHome = 3;
			if (m.getScoreHomeTeam() - m.getScoreVisitorTeam() >= 3) {
				pointsHome++;
			}
		} else if (m.getScoreVisitorTeam() > m.getScoreHomeTeam()) {
			pointsVisitor = 3;
			if (m.getScoreVisitorTeam() - m.getScoreHomeTeam() >= 3) {
				pointsVisitor++;
			}
		} else {
			pointsVisitor = pointsHome = 1;
		}
		
		if (pointsHome >= 3) {
			homeP1.addWin();
			visitorP1.addLoss();
			if (homeP2 != null)
				homeP2.addWin();
			if (visitorP2 != null)
				visitorP2.addLoss();
		} else if (pointsHome == 1) {
			homeP1.addDraw();
			visitorP1.addDraw();
			if (homeP2 != null)
				homeP2.addDraw();
			if (visitorP2 != null)
				visitorP2.addDraw();
		} else {
			homeP1.addLoss();
			visitorP1.addWin();
			if (homeP2 != null)
				homeP2.addLoss();
			if (visitorP2 != null)
				visitorP2.addWin();
		}
		
		if (m.getMatchType().equalsIgnoreCase(Match.MATCH_TYPE_CHAMPIONSHIP)) {
			pointsHome++;
			pointsVisitor++;
		}
		
		if (homeP1 != null) {
			homeP1.setGames(homeP1.getGames()+1);
			homeP1.addPoints(pointsHome);
			homeP1.addGoalsScored(m.getScoreHomeTeam());
			homeP1.addGoalsConceded(m.getScoreVisitorTeam());
			homeP1.calculatePointsAverage();
			if (initialData) {
				homeP1.setWinsInARow(0);
				homeP1.setLosesInARow(0);
			}
			savePlayer(homeP1);
		}
		
		if (homeP2 != null) {
			homeP2.setGames(homeP2.getGames()+1);
			homeP2.addPoints(pointsHome);
			homeP2.addGoalsScored(m.getScoreHomeTeam());
			homeP2.addGoalsConceded(m.getScoreVisitorTeam());
			homeP2.calculatePointsAverage();
			if (initialData) {
				homeP2.setWinsInARow(0);
				homeP2.setLosesInARow(0);
			}
			savePlayer(homeP2);
		}
		
		if (visitorP1 != null) {
			visitorP1.setGames(visitorP1.getGames()+1);
			visitorP1.addPoints(pointsVisitor);
			visitorP1.addGoalsScored(m.getScoreVisitorTeam());
			visitorP1.addGoalsConceded(m.getScoreHomeTeam());
			visitorP1.calculatePointsAverage();
			if (initialData) {
				visitorP1.setWinsInARow(0);
				visitorP1.setLosesInARow(0);
			}
			savePlayer(visitorP1);
		}
		
		if (visitorP2 != null) {
			visitorP2.setGames(visitorP2.getGames()+1);
			visitorP2.addPoints(pointsVisitor);
			visitorP2.addGoalsScored(m.getScoreVisitorTeam());
			visitorP2.addGoalsConceded(m.getScoreHomeTeam());
			visitorP2.calculatePointsAverage();
			if (initialData) {
				visitorP2.setWinsInARow(0);
				visitorP2.setLosesInARow(0);
			}
			savePlayer(visitorP2);
		}
	}
	
	public static List<Player> getPlayersToRanking() {
		List<Player> players;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Query q = null;
		try {
		q = pm.newQuery(Player.class);
		q.setFilter("games >= 5");
//		q.setOrdering("average desc");
		players = (List<Player>) q.execute();
		} finally {
			if (q != null)
				q.closeAll();
		}
		
		Collections.sort(players, new Comparator<Player>() {

			@Override
			public int compare(Player o1, Player o2) {
				if (o1.getAverage() < o2.getAverage())
					return 1;
				else if (o2.getAverage() < o1.getAverage())
					return -1;
				else
					return 0;
			}
		});
		return players;
	}
}